package com.yzz.composeweatherproject.http.jsoup

import com.yzz.composeweatherproject.entity.*
import com.yzz.composeweatherproject.http.DomainNameUtils
import org.jsoup.Jsoup
import org.jsoup.nodes.Document

class JsoupUtils {
    companion object {
        fun getWeatherData(data: String): ArrayList<WeatherProvinceBean?> {
            val provinceList = arrayListOf<WeatherProvinceBean?>()
            val document: Document = Jsoup.parse(data)
            val elements = document.select("div[class=wrap1100]")
                .select("div[class=wrapbox]")
                .select("div[class=citybox]")
            elements.forEach { els ->
                //省份列表
                val provinceElements = els.select("h2")
                    .select("a")
                //城市列表
                val cityElements = els.select("span")
                provinceElements.forEach { pSels ->
                    //获取省份列表信息
                    val name = pSels.text()
                    val url = pSels.attr("href")
                    provinceList.add(WeatherProvinceBean(name, url, arrayListOf()))
                }
                cityElements.forEachIndexed { index, element ->
                    //获取城市列表的列表信息
                    element.select("a").forEach { cEls ->
                        //获取城市的列表信息
                        val name = cEls.text()
                        val url = cEls.attr("href")
                        provinceList[index]?.cityNameList?.add(WeatherCityBean(name, url))
                    }
                }
            }

            return provinceList
        }

        fun getWeatherDetailedData(data: String?): WeatherDetailedBean {
            val provinceList = WeatherDetailedBean()
            val document: Document = Jsoup.parse(data)
            val elements = document.select("div[class=weatherbox]")
                .select("div[class=wrap1100]")
                .select("div[class=left]")
                .select("dl[class=weather_info]")
            val url = elements
                .select("dt")
                .select("img")
                .attr("src")
            val name = elements.select("dd[class=name]")
                .select("h1")
                .text()
            val time = elements.select("dd[class=week]")
                .text()
            val weatherElements = elements.select("dd[class=weather]")
            val weatherState = weatherElements.select("span")
                .text()
            val temperature = weatherElements.select("p[class=now]")
                .text()
            val humidity = weatherElements.select("dd[class=shidu]")
                .text()

            provinceList.cityImgUrl = url
            provinceList.name = name
            provinceList.time = time
            provinceList.weatherState = weatherState
            provinceList.temperature = temperature
            provinceList.humidity = humidity
            return provinceList
        }

        fun getLocationData(data: String?): LocationBean {
            val locationBean = LocationBean()
            val document: Document = Jsoup.parse(data)
            val cityWeatherDocument = document.select("body[id=body]")
//            由于这个字段不稳定，就直接不用这个
//                .select(getDivSelect("city_weather cbg_b8 citydt"))

            val cityName =
                cityWeatherDocument.select(getDivSelect("hhx_index_newHead clearfix"))
                    .select(getDivSelect("hhx_index_newHead_l"))
                    .select("text")
                    .text()
            val airQuality = cityWeatherDocument.select(getDivSelect("info")).text()


            val time = cityWeatherDocument.select(getDivSelect("date")).text()
            val tempDocument = cityWeatherDocument.select(getSelect("dl", "temp"))
            val weatherUrl = tempDocument.select("dt")
                .select("img")
                .attr("src")
            val temperature = tempDocument.select(getSelect("dd", "now"))
                .text()
            val weatherState = tempDocument.select(getSelect("dd", "txt"))
                .text()
            locationBean.name = cityName
            locationBean.time = time
            locationBean.imgUrl = DomainNameUtils.weatherPeUrl + weatherUrl
            locationBean.weatherState = weatherState
            locationBean.temperature = temperature
            locationBean.airQuality = airQuality

            val fo24title = document.select("body[id=body]")
                .select(getSelect("p", "hhx_newAllDayTit")).text()
            locationBean.fo24title = fo24title
            //24小时预报
            document.select("body[id=body]")
                .select(getSelect("ul", "hhx_newAllDayUl")).select("li")
                .forEach {
                    val time = it.select("p").text()
                    var imgUrl = it.select("img").attr("src")
                    val temperature = it.select("span").text()
                    imgUrl = getWeatherImageUrl(imgUrl)
                    locationBean.forecast24Bean.add(
                        Forecast24Bean(time, imgUrl, temperature)
                    )
                }
            //未来几天天气预报
            document.select("body[id=body]")
                .select(getDivSelect("weather_week"))
                .select("dl")
                .forEach {
                    val el = it.select("a")
                    val time = el.select("a")
                        .select("dt").text()
                    var date = ""
                    var weatherTemperature = ""

                    el.select(getSelect("dd", "txt")).forEachIndexed { index, element ->
                        if (index == 0) {
                            date = element.text()
                        } else if (index == 1) {
                            weatherTemperature = element.text()
                        }
                    }
                    var imgUrl = el.select(getSelect("dd", "img")).select("img").attr("src")
                    imgUrl = getWeatherImageUrl(imgUrl)
                    val weatherState = el.select(getSelect("dd", "txt2")).text()
                    val airQuality = el.select(getSelect("dd", "txt3")).text()
                    locationBean.forecastAfterDay.add(
                        ForecastAfterDay(
                            time,
                            date,
                            imgUrl,
                            weatherState,
                            weatherTemperature,
                            airQuality
                        )
                    )
                }


//            locationBean.humidity = weatherHim1 + weatherHim2
            return locationBean
        }

        private fun getWeatherImageUrl(url: String?): String {
            if (url == null || url.isEmpty()) {
                return ""
            }
            val urlIndex = url.lastIndexOf(".com/")
            return DomainNameUtils.weatherPeUrl + url.substring(urlIndex + 5)
        }

        private fun getDivSelect(three: String): String {
            return "div[class=$three]"
        }


        private fun getSelect(one: String, three: String): String {
            return "$one[class=$three]"
        }
    }


}